package com.linlang.grasharepro.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Switch;
import android.widget.TextView;

import com.linlang.grasharepro.R;

/**
 * Created by lixin on 2016-8-8.
 */
public class EditActivity extends Activity {

    private TextView tv_aboutUs;
    private ImageView user_info_back;
    private View parentView;
    private Switch s_switch;
    private LinearLayout ll_msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_edit);
        initView();
    }

    private void initView() {

        tv_aboutUs = (TextView) findViewById(R.id.tv_aboutUs);
        tv_aboutUs.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                showPopupWindow(view);
            }
        });

        //后退按钮
        user_info_back = (ImageView) findViewById(R.id.user_info_back);
        user_info_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditActivity.this.finish();
            }
        });

        ll_msg = (LinearLayout) findViewById(R.id.ll_msg);
        s_switch = (Switch) findViewById(R.id.s_switch);
        s_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){//开启
                    ll_msg.setVisibility(View.VISIBLE);
                }else{//关闭
                    ll_msg.setVisibility(View.GONE);
                }
            }
        });
    }

    private void showPopupWindow(View view) {
        View parentView = LayoutInflater.from(this).inflate(
                R.layout.activity_person_edit, null);

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.about_us, null);

        final PopupWindow popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.list_background_imgbg));

        // 设置好参数之后再show
        popupWindow.showAtLocation(parentView, Gravity.CENTER, 60, 0);
    }

}
